package cn.tedu.justone.pack02;

/**
 * ZZ：JustOne
 * VX：Andylau2020smile
 * DY：JustOne2018
 * ZT：《》
 */
public class Demo14_1_案例测试wait被提前唤醒 {
    final static String LOCK = "刘景景";

    //测试：测试wait被提前唤醒
    //结果：被唤醒后正常执行后面的语句
    public static void main(String[] args) {

        Thread t1 = new Thread(){
            @Override
            public void run() {
                synchronized (LOCK){
                    System.out.println("开始 wait()");
                    try {
                        System.out.println("1");
                        LOCK.wait(9000);
                        System.out.println("2");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("over");
                }
            }
        };
        t1.start();

        //休眠2秒后唤醒原计划等待9秒的线程
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        //执行提前唤醒动作
        synchronized (LOCK){
            LOCK.notify();
        }

    }
}
